package com.gulimall.gulimallorder.web;

import com.gulimall.gulimallorder.service.OrderService;
import com.gulimall.gulimallorder.vo.OrderConfirmVo;
import com.gulimall.gulimallorder.vo.OrderSubmitVo;
import com.gulimall.gulimallorder.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {
    @Autowired
    OrderService orderService;
    @GetMapping("/toTrade")
    public String totrade(Model model) throws ExecutionException, InterruptedException {
       OrderConfirmVo orderConfirmVo= orderService.confirmOrder();
       model.addAttribute("orderConfirmData",orderConfirmVo);
        return "confirm";
    }

    /**
     * 下单
     * @param vo
     * @return
     */
    @PostMapping("/submitOrder")
    public String Submitorder(OrderSubmitVo vo){
        SubmitOrderResponseVo responseVo=orderService.submitOrder(vo);
        //下单，创建订单，校验令牌，校验价格，锁库存

        //成功去支付页面
        if(responseVo.getCode()==0){
            return "pay";
        }
        else{
            //下单失败回到订单确认页重新确认订单信息
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }
}
